package com.demo.http;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.demo.http.androidnative.HttpUtil;
import com.demo.http.okhttp.HttpClient;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    private static final String TRANS_API_HOST = "http://api.fanyi.baidu.com/api/trans/vip/translate";

    private Map<String, String> mParams = null;

    private RadioGroup mRadioGroup = null;
    private RadioButton mRadioButton1 = null;
    private RadioButton mRadioButton2 = null;

    private Button mButtonGet = null;
    private Button mButtonPost = null;

    private TextView mTextView = null;

    private static final int USING_HTTPURLCONNECTION = 0;

    private static final int USING_OKHTTP = 1;

    private int mUsingFlag = USING_HTTPURLCONNECTION;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mParams = new HashMap<>();
        mParams.put("q", "apple");
        mParams.put("from", "en");
        mParams.put("to", "zh");
        mParams.put("appid", "2015063000000001");
        // 随机数
        mParams.put("salt", "1435660288");
        // 签名
        mParams.put("sign", "f89f9594663708c1605f3d736d01d2d4");

        mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        mRadioButton1 = (RadioButton) findViewById(R.id.radioButton1);
        mRadioButton2 = (RadioButton) findViewById(R.id.radioButton2);
        mButtonGet = (Button) findViewById(R.id.btn_get);
        mButtonPost = (Button) findViewById(R.id.btn_post);
        mTextView = (TextView) findViewById(R.id.textView);
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == mRadioButton1.getId()) {
                    Log.i(TAG, "httpurlconnection");
                    mUsingFlag = USING_HTTPURLCONNECTION;
                } else if (checkedId == mRadioButton2.getId()) {
                    Log.i(TAG, "okhttp");
                    mUsingFlag = USING_OKHTTP;
                } else {
                    Log.i(TAG, "nothing checked");
                }
            }
        });
        mRadioButton1.setChecked(true);

        mButtonGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTextView.setText(null);
                if (USING_HTTPURLCONNECTION == mUsingFlag) {
                    HttpUtil.getInstance().get(TRANS_API_HOST, mParams, new HttpCallback() {
                        @Override
                        public void onFailure(final int errorCode, final Exception e) {
                            if (0 != errorCode) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        mTextView.setText("errorCode = " + errorCode);
                                    }
                                });
                            }
                            if (null != e) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        mTextView.setText(e.toString());
                                    }
                                });
                            }
                        }

                        @Override
                        public void onResponse(byte[] responseBody) {
                            final String result = StringUtil.decodeUnicode(new String(responseBody));
                            Log.e(TAG, "get : " + result);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mTextView.setText(result);
                                }
                            });
                        }
                    });
                } else {
                    HttpClient.getInstance().get(TRANS_API_HOST, mParams, new HttpCallback() {

                        public void onFailure(final int errorCode, final Exception e) {
                            if (0 != errorCode) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        mTextView.setText("errorCode = " + errorCode);
                                    }
                                });
                            }
                            if (null != e) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        mTextView.setText(e.toString());
                                    }
                                });
                            }
                        }

                        @Override
                        public void onResponse(byte[] responseBody) {
                            final String result = StringUtil.decodeUnicode(new String(responseBody));
                            Log.e(TAG, "get : " + result);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mTextView.setText(result);
                                }
                            });
                        }
                    });
                }
            }
        });
        mButtonPost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTextView.setText(null);
                if (USING_HTTPURLCONNECTION == mUsingFlag) {
                    HttpUtil.getInstance().post(TRANS_API_HOST, mParams, new HttpCallback() {
                        @Override
                        public void onFailure(final int errorCode, final Exception e) {
                            if (0 != errorCode) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        mTextView.setText("errorCode = " + errorCode);
                                    }
                                });
                            }
                            if (null != e) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        mTextView.setText(e.toString());
                                    }
                                });
                            }
                        }

                        @Override
                        public void onResponse(byte[] responseBody) {
                            final String result = StringUtil.decodeUnicode(new String(responseBody));
                            Log.e(TAG, "post : " + result);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mTextView.setText(result);
                                }
                            });
                        }
                    });
                } else {
                    HttpClient.getInstance().post(TRANS_API_HOST, mParams, new HttpCallback() {

                        public void onFailure(final int errorCode, final Exception e) {
                            if (0 != errorCode) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        mTextView.setText("errorCode = " + errorCode);
                                    }
                                });
                            }
                            if (null != e) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        mTextView.setText(e.toString());
                                    }
                                });
                            }
                        }

                        @Override
                        public void onResponse(byte[] responseBody) {
                            final String result = StringUtil.decodeUnicode(new String(responseBody));
                            Log.e(TAG, "get : " + result);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mTextView.setText(result);
                                }
                            });
                        }
                    });
                }
            }
        });
    }
}
